package com.xm2.demo.system.center.controller;

import com.xm2.demo.model.Center;
import com.xm2.demo.model.Stores;
import com.xm2.demo.system.center.dto.CenterInfo;
import com.xm2.demo.system.center.dto.CenterPage;
import com.xm2.demo.system.center.service.CenterService;
import com.xm2.demo.system.shop.dto.StoresPage;
import com.xm2.demo.utils.JsonResult;
import com.xm2.demo.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @Create
 * @create 2018-12-12 14:15
 * @Description：洗衣中心控制器
 */
@Controller
@RequestMapping(value = "/system/")
public class CenterController {

    @Autowired
    private CenterService centerService;

    @GetMapping(value = "center")
    public String center(Model model){
        Pagination pagination = new Pagination();
        pagination = centerService.findByPageable(pagination);
        model.addAttribute("pagination",pagination);
        return "center";
    }

    @PostMapping(value = "center")
    @ResponseBody
    public Pagination doCenter(@RequestBody CenterPage centerPage){
        Pagination pagination;
        if (centerPage.getCenterName().equals("")){
            pagination = centerService.findByPageable(centerPage);
        } else {
            pagination = centerService.findByCenterName(centerPage);
        }
        return pagination;
    }

    @PostMapping("addCenter")
    @ResponseBody
    public JsonResult addCenter(@RequestBody CenterInfo center){
        if (center.getCenterName().equals("")){
            return new JsonResult("101","洗衣中心名不能为空!");
        } else if(center.getCenterAddress().equals("")){
            return new JsonResult("102","地址不能为空!");
        } else {
            centerService.addCenter(center);
            return new JsonResult();
        }
    }

    @PostMapping(value = "del_ids")
    @ResponseBody
    public JsonResult delByIds(@RequestBody String ids){
       try{
           centerService.deleteByIds(ids);
           return new JsonResult();
       }catch (Exception e){
           return new JsonResult("103","删除失败");
       }

    }

    @PostMapping(value = "getCenter")
    @ResponseBody
    public List<String> getCenter(){
        List<Center> centerList = centerService.findAll();
        List<String> stringList = new ArrayList<>();
        for (Center center : centerList) {
            stringList.add(center.getCenterName());
        }

        return stringList;
    }


}
